/**
 * 
 */
package controller.xml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * A class to handle event in the parsing of a session exported in XML.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public abstract class XMLHandler {

	protected XMLHandler	delegate	= null;
	protected StringBuffer	buffer		= new StringBuffer();


	/**
	 * @see DefaultHandler, do the same but can delegate the event.
	 * @return if the handler handle this event
	 * @throws SAXException
	 *             if there is a problem
	 */
	public boolean characters(char[] ch, int start, int length) throws SAXException {
		if (delegate != null)
			if (!delegate.characters(ch, start, length))
				return characters2(ch, start, length);
			else
				return true;

		return characters2(ch, start, length);
	}

	/**
	 * @see DefaultHandler, do the same but can delegate the event.
	 * @return if the handler handle this event
	 * @throws SAXException
	 *             if there is a problem
	 */
	public boolean endDocument() throws SAXException {
		if (delegate != null)
			if (!delegate.endDocument())
				return endDocument2();
			else
				return true;

		return endDocument2();
	}

	/**
	 * @see DefaultHandler, do the same but can delegate the event.
	 * @return if the handler handle this event
	 * @throws SAXException
	 *             if there is a problem
	 */
	public boolean endElement(String uri, String localName, String qName) throws SAXException {
		if (delegate != null)
			if (!delegate.endElement(uri, localName, qName))
				return endElement2(uri, localName, qName);
			else
				return true;

		return endElement2(uri, localName, qName);
	}

	/**
	 * @see DefaultHandler, do the same but can delegate the event.
	 * @return if the handler handle this event
	 * @throws SAXException
	 *             if there is a problem
	 */
	public boolean startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
		if (delegate != null)
			if (!delegate.startElement(arg0, arg1, arg2, arg3))
				return startElement(arg0, arg1, arg2, arg3);
			else
				return true;

		buffer.setLength(0);
		return startElement2(arg0, arg1, arg2, arg3);

	}

	/**
	 * @see DefaultHandler
	 * @return if the handler handle this event
	 * @throws SAXException
	 *             if there is a problem
	 */
	public boolean characters2(char[] ch, int start, int length) throws SAXException {
		buffer.append(new String(ch, start, length));
		return true;
	}

	/**
	 * @see DefaultHandler
	 * @return if the handler handle this event
	 * @throws SAXException
	 *             if there is a problem
	 */
	public abstract boolean endDocument2() throws SAXException;

	/**
	 * @see DefaultHandler
	 * @return if the handler handle this event
	 * @throws SAXException
	 *             if there is a problem
	 */
	public abstract boolean endElement2(String uri, String localName, String qName) throws SAXException;

	/**
	 * @see DefaultHandler
	 * @return if the handler handle this event
	 * @throws SAXException
	 *             if there is a problem
	 */
	public abstract boolean startElement2(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException;

}
